﻿#include "InstructionTurnTableRun.h"

#include "TurnTableCodec.h"
#include "ScriptRunner.h"
#include "Device.h"
#include "StateChecker.h"

__SCRIPTINSTRUCTION_REGISTER(InstructionTurnTableRun,ScriptInstruction::Instruction_TurnTable_Run,&InstructionTurnTableRun::fromByteArray1);

InstructionTurnTableRun::InstructionTurnTableRun()
{
  instructionFlag = Instruction_TurnTable_Run;
}

InstructionTurnTableRun::InstructionTurnTableRun(InstructionTurnTableRun const& instructionTurnTableRun)
{
  *this=instructionTurnTableRun;
}

InstructionTurnTableRun& InstructionTurnTableRun::operator=(InstructionTurnTableRun const & instructionTurnTableRun)
{
  *(ScriptInstruction*)this=instructionTurnTableRun;
  runningMode=instructionTurnTableRun.runningMode;
  return *this;
}

InstructionTurnTableRun::~InstructionTurnTableRun()
{}

void InstructionTurnTableRun::run()
{
  scriptRunner->sendInstruction(deviceId,
                                TurnTableCodec::TurnTable_Run,
                                runningMode?TurnTableCodec::Mode_RateMode:TurnTableCodec::Mode_PositionMode);
  StateChecker<bool>::next_ifConditionReached(&Device::getTurnTable_runningState_running,true,deviceId,scriptRunner);
}

QString InstructionTurnTableRun::toString()
{
  return QStringLiteral("转台运行%1").arg(runningMode?"速率模式":"位置模式");
}

QByteArray InstructionTurnTableRun::toByteArray()
{
  return QByteArray().append((char*)&runningMode,sizeof (runningMode));
}

ScriptInstruction* InstructionTurnTableRun::fromByteArray1(char * const src,int size,int * offset)
{
  if((size-*offset)<byteArraySize)
    {
      return Q_NULLPTR;
    }
  InstructionTurnTableRun* instructionTurnTableRun=new InstructionTurnTableRun();
  instructionTurnTableRun->setRunningMode(*(bool*)(src+*offset));
  offset+=sizeof (runningMode);
  return instructionTurnTableRun;
}

bool InstructionTurnTableRun::getRunningMode()
{
  return runningMode;
}

InstructionTurnTableRun* InstructionTurnTableRun::setRunningMode(bool runningMode)
{
  this->runningMode=runningMode;
  return this;
}
